class EventEmitter{
    constructor(){
        this.events = {}; 
    }

    on(type, fn){
        if(this.events[type]){
            this.events[type].push(fn);
        }else {
            this.events[type] = [fn];
        }
    }

    emmit(type){
        if(this.events[type]){
            this.events[type].forEach(fn => {
                fn();
            });
        }
       
    }

    off(type,fn){
        if(this.events[type]){
            this.events[type] = this.events[type].filter(item => item !== fn);
        }
    }
}

let emitter = new EventEmitter();

// 订阅事件

emitter.on('have',()=>{
    console.log('买111');
})

const fn = (()=>{
    console.log('买222');
})

emitter.on('have',fn)

// 发布事件
emitter.emmit('have')
emitter.emmit('has')

//取消订阅